//
//  countdownModel.m
//  HaoHint
//
//  Created by winter on 16/5/23.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "CountdownModel.h"

@interface CountdownModel ()
@property (nonatomic, copy) NSString *remindString;
@property (nonatomic, assign) NSInteger timeSecond;
@property (nonatomic, copy) NSString *countdownID;
@property (nonatomic, copy) NSString *soundTitle;
@end

@implementation CountdownModel

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.timeTitle forKey:@"timeTitle"];
    [aCoder encodeObject:self.alertBody forKey:@"alertBody"];
    [aCoder encodeObject:self.preSoundName forKey:@"preSoundName"];
    [aCoder encodeObject:self.soundName forKey:@"soundName"];
    [aCoder encodeObject:self.countdownTime forKey:@"countdownTime"];
    
    [aCoder encodeInteger:self.remindEarlyType forKey:@"remindEarlyType"];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        self.timeTitle = [aDecoder decodeObjectForKey:@"timeTitle"];
        self.alertBody = [aDecoder decodeObjectForKey:@"alertBody"];
        self.preSoundName = [aDecoder decodeObjectForKey:@"preSoundName"];
        self.soundName = [aDecoder decodeObjectForKey:@"soundName"];
        self.countdownTime = [aDecoder decodeObjectForKey:@"countdownTime"];
        self.remindEarlyType = [aDecoder decodeIntegerForKey:@"remindEarlyType"];
    }
    return self;
}

- (void)setRemindEarlyType:(CountdownModelRemindStyle)remindEarlyType
{
    _remindEarlyType = remindEarlyType;
    
    NSString *remindType = nil;
    if (remindEarlyType > 0) {
        remindType = [NSString stringWithFormat:@"%ld分钟前",(long)remindEarlyType];
    }
    else if (remindEarlyType > 30) {
        remindType = [NSString stringWithFormat:@"%ld小时前",(remindEarlyType/(long)60)];
    }
    if (remindType) {
        self.remindString = [NSString stringWithFormat:@"%@\n计时结束时",remindType];
        self.preRemind = YES;
    }
    else {
        self.remindString = @"计时结束时";
        self.preRemind = NO;
    }
}

- (void)setSoundName:(NSString *)soundName
{
    _soundName = [soundName copy];
    self.soundTitle = [NSString stringWithFormat:@"计时结束时 （%@）",soundName];
}

- (void)setCountdownTime:(NSString *)countdownTime
{
    _countdownTime = [countdownTime copy];
    
    NSArray *objs = [countdownTime componentsSeparatedByString:@":"];
    
    NSInteger HValue = 0;
    NSInteger MValue = 0;
    NSInteger SValue = 0;
    
    if (objs.count == 3) {
        HValue = [objs[0] integerValue];
        MValue = [objs[1] integerValue];
        SValue = [objs[2] integerValue];
        
        MValue += HValue*60;
        SValue += MValue*60;
    }
    else if (objs.count == 2) {
        MValue = [objs[0] integerValue];
        SValue = [objs[1] integerValue];
        
        SValue += MValue*60;
    }
    else {
        SValue = [objs[0] integerValue];
    }
    self.timeSecond = SValue;
    
    NSDate *date = [NSDate date];
    self.countdownID = [NSString stringWithFormat:@"%f",date.timeIntervalSince1970];
}

@end
